package cn.com.headfree.base.service;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import com.aostarit.smcrypto.Sm2Utils;
import com.gta.edu.sdk.util.SpringContextHolder;



/**
 * Title: Description:统一权限平台接口调用 Copyright: Copyright (c) 2017 Company:
 * www.headfree.com.cn
 * 
 * @author fengya
 * @version 1.0
 * @date 2019年8月9日 下午2:55:08
 */
//@Component
public class IscSsoCall {

	private static final Logger LOG = Logger.getLogger(IscSsoCall.class);
	
    private static Environment environment = null;

	private static String APPID;

//	@Value("${isc.sso.appId}")
//	public void setAPPID(String iscSsoAppId) {
//		IscSsoCall.APPID = iscSsoAppId;
//	}
//
	private static String KEY;
//
//	@Value("${isc.sso.key}")
//	public void setKEY(String iscSsoKey) {
//		IscSsoCall.KEY = iscSsoKey;
//	}

	private static String iscFrontmvServ;

	@Value("${isc.sso.iscFrontmvServ}")
	public void setISCFRONTMVSERV(String iscSsoIscFrontmvServ) {
		IscSsoCall.iscFrontmvServ = iscSsoIscFrontmvServ;
	}

	private static HttpClient client = new HttpClient();

	public static synchronized String call(String url,String modulTag) throws Exception {
		if (environment == null) {
			environment = SpringContextHolder.getBean(Environment.class);
		}
		KEY = environment.getProperty("isc.sso." + modulTag + ".key");
		APPID = environment.getProperty("isc.sso." + modulTag + ".appId");
		String token = getToken();// 获取token
		if (StringUtils.isEmpty(token)) {
			throw new Exception("获取token为空！");
		}
		return IscSsoCall.appInfoApi(token, APPID, url);
	}

	private static String getToken() throws Exception {
		Sm2Utils sm2Utils = new Sm2Utils();
		String token = sm2Utils.encryptToASN1(KEY, APPID + ":" + System.currentTimeMillis());// 使用国网sm2加密算法加密参数且服务器时间相差不能超过5分钟 如果超过5分钟将会返回请求超时
		PostMethod post = new PostMethod(iscFrontmvServ + "domain/auth");

		post.addParameter(new NameValuePair("appId", APPID));
		post.addRequestHeader(new Header("app_token", token));// 将生成的加密串放到header
		client.executeMethod(post);

		if (post.getStatusCode() == HttpStatus.SC_OK) {
			JSONObject jsonObject = new JSONObject(post.getResponseBodyAsString());
			return jsonObject.getString("access_token");
		} else {
			if (post.getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
				throw new RuntimeException("token超时!");
			}
			if (post.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
				throw new RuntimeException("缺少参数!");
			}
		}
		return null;
	}

	private static String appInfoApi(String token, String appid, String url) throws Exception {
		PostMethod post = new PostMethod(iscFrontmvServ + "v1/api/" + url);
		post.addRequestHeader(new Header("appId", APPID));// 应用id 放到header
		post.addRequestHeader(new Header("app_token", token));// 和应用id配对的访问token 放到header
		post.setRequestHeader("charset", "utf-8");
		client.executeMethod(post);
		return post.getResponseBodyAsString();
	}
}
